package com.shujia.core.transformations.kv

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object MapValuesOpt {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf()
    conf.setMaster("local")
    conf.setAppName("union合并")

    val sc = new SparkContext(conf)

    val nameWithAgeKVRDD: RDD[(String, Int)] = sc.textFile("spark/data/students.txt")
      .map(_.split(",")) // [xxx,xxx,xx,xx,xxx]
      .map {
        case Array(_, name: String, age: String, _, _) => (name, age.toInt)
      }

    //    nameWithAgeKVRDD.map((kv: (String, Int)) => (kv._1, kv._2 + 100))
    //      .foreach(println)

    /**
     * mapValue,只能作用在键值RDD上，主要作用是，对每一个键值对数据的值进行处理，键不变，
     */
    nameWithAgeKVRDD.mapValues(_ + 100)
      .foreach(println)


  }
}
